﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace IWMAG.Core.EnhancedProperties.PropertyNodes
{
#if false
    internal class DatabindingNode<T> : PropertyNode<T>
    {
        private PropertyContainerBase source;
        private PropertyBacker<T> backer;

        T currentValue;

        public DatabindingNode(int priority, PropertyContainerBase sourceObject, string propertyName)
            : base(priority)
        {
            if (sourceObject is PropertyContainerBase)
            {
                SetupPropertyContainerBase(sourceObject as PropertyContainerBase, propertyName);
                
            }
            else
            {
                throw new NotSupportedException();
            }
        }

        private void SetupPropertyContainerBase(PropertyContainerBase propertyContainerBase, string propertyName)
        {
            backer = PropertyManager.GetPropertyBackerWithName(propertyContainerBase.GetType(), propertyName) as PropertyBacker<T>;

            if (backer == null)
            {
                throw new MissingFieldException();
            }

            source = propertyContainerBase;

            currentValue = propertyContainerBase.GetValue(backer);

            source.SubscribeNotifyChanging(backer, OnChanging);
            source.SubscribeNotifyChanged(backer, OnChange);
            

        }

        private void OnChange(T val)
        {
            currentValue = val;
            Previous.SuccessorChanged(val);

        }

        private void OnChanging(T val)
        {
            Previous.SuccessorChanging(val);
        }


        public override T GetNodeValue()
        {
            return currentValue;
        }

        public override void SuccessorChanged(T value) { }

        public override void SuccessorChanging(T value) { }

        public override void Dispose()
        {
            base.Dispose();

            source.UnsubscribeNotifyChanging(backer, OnChanging);
            source.UnsubscribeNotifyChanged(backer, OnChange);
            source = null;
            backer = null;
        }
    }
#endif
}
